﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security;
using SPlusres.Business;

namespace SPlusres.Business.Common
{
    /// <summary>
    /// User Info Data Business
    /// </summary>
    public class UserInfo
    {
        /// <summary>
        /// Check User
        /// </summary>
        /// <param name="User">Username</param>
        /// <param name="Password">Password</param>
        /// <returns>Message</returns>
        public static Models.CallResult Login(string User, string Password)
        {
            using (var data = new Data.SPlusresDataContext(SystemSetting.ConnectionString))
            {
                var userinfo = data.UserInfo.Where(m => m.UserName == User).FirstOrDefault();

                // Check User Is Exist
                if (userinfo == null)
                    return new Models.CallResult(string.Format(Properties.Resources.UserNotExistForUserName, User));

                // Check Password Is right
                if (userinfo.Password != Password.Password(PasswordFormat.MD5))
                    return new Models.CallResult(Properties.Resources.PasswordError);

                // Return UserInfo
                return new Models.CallResult(new
                {
                    userid = DES.Encode(userinfo.ID.ToString(), SystemSetting.EncryptKey)
                });
            }
        }

        /// <summary>
        /// Change Password
        /// </summary>
        /// <param name="User">User</param>
        /// <param name="Old">Old Password</param>
        /// <param name="New">new Password</param>
        /// <returns>Message</returns>
        public static Models.CallResult ChangePassword(int User, string Old, string New)
        {
            using (var data = new Data.SPlusresDataContext(SystemSetting.ConnectionString))
            {
                var userinfo = data.UserInfo.Where(m => m.ID == User).FirstOrDefault();

                // Check User Is Exist
                if (userinfo == null)
                    return new Models.CallResult(string.Format(Properties.Resources.UserNotExistForUserName, User));

                // Check Password Is right
                if (userinfo.Password != Old.Password(PasswordFormat.MD5))
                    return new Models.CallResult(Properties.Resources.OldPasswordError);

                // ChangePass
                userinfo.Password = New.Password(PasswordFormat.MD5);
                data.SubmitChanges();

                // Return Message
                return new Models.CallResult();
            }
        }

        /// <summary>
        /// Register User
        /// </summary>
        /// <param name="model">Model</param>
        /// <returns>Message</returns>
        public static Models.CallResult Register(Data.UserInfo model)
        {
            using (var data = new Data.SPlusresDataContext(SystemSetting.ConnectionString))
            {
                // Check User Is Exist
                if (data.UserInfo.Where(m => m.UserName == model.UserName).Count() == 1)
                    return new Models.CallResult(string.Format(Properties.Resources.UserExist, model.UserName));

                if (data.UserInfo.Where(m => m.Email == model.Email).Count() == 1)
                    return new Models.CallResult(Properties.Resources.EmailExist);

                var userinfo = new Data.UserInfo()
                {
                    Password = model.Password.Password(PasswordFormat.MD5),
                    UserName = model.UserName,
                    CreateTime = DateTime.Now,
                    Email = model.Email
                };

                data.UserInfo.InsertOnSubmit(userinfo);
                data.SubmitChanges();

                // Return Message
                return new Models.CallResult();
            }
        }

        /// <summary>
        /// Modify UserInfo
        /// </summary>
        /// <param name="model">Model</param>
        /// <returns>Message</returns>
        public static Models.CallResult Info(Data.UserInfo model)
        {
            using (var data = new Data.SPlusresDataContext(SystemSetting.ConnectionString))
            {
                var userinfo = data.UserInfo.Where(m => m.ID == model.ID).FirstOrDefault();

                // Check User Is Exist
                if (userinfo == null)
                    return new Models.CallResult(string.Format(Properties.Resources.UserNotExistForUserName, model.UserName));

                userinfo.Birthday = model.Birthday;
                userinfo.Description = model.Description;
                userinfo.QQ = model.QQ;
                userinfo.NickName = model.NickName;
                userinfo.Sex = model.Sex;
                userinfo.Mobile = model.Mobile;
                userinfo.Job = model.Job;
                userinfo.Area = model.Area;
                userinfo.Email = model.Email;

                data.SubmitChanges();

                // Return Message
                return new Models.CallResult();
            }
        }

        /// <summary>
        /// Read User id Of Userinfo
        /// </summary>
        /// <param name="id">user flag</param>
        /// <returns>userinfo</returns>
        public static Data.UserInfo Info(int id)
        {
            using (var data = new Data.SPlusresDataContext(SystemSetting.ConnectionString))
            {
                return data.UserInfo.Where(m => m.ID == id).Select(m => new Data.UserInfo()
                {
                    UserName = m.UserName,
                    Birthday = m.Birthday,
                    Description = m.Description,
                    QQ = m.QQ,
                    Mobile = m.Mobile,
                    NickName = m.NickName,
                    Sex = m.Sex,
                    Email = m.Email,
                    Area = m.Area,
                    Job = m.Job,
                    CreateTime = m.CreateTime
                }).FirstOrDefault();
            }
        }
    }
}
